import {Form, InputNumber} from 'antd'
import {concat, omit} from 'ramda'
import Group from './Group'

const FormItem = Form.Item;

// type CheckboxPropsType = {
//     initialValue?: boolean,
//     name: string,
//     max?: number,
//     min?: number,
//     step?: number,
//     width?: string,
//     labelCol?: number,
//     disabled?: boolean,
//     placeholder?: string,
//     span?: number,
//     textAlign?: 'riught' | 'cxenter' | 'left',
//     required?: boolean,
//     getFieldValue?: Function,
//     getFieldDecorator?: Function,
//     max?: number,
//     min?: number,
//     rules?: any,
//     label?: string | number
// }


const FormInputNumber = ({disabled, span, textAlign, rules, required, getFieldValue, width, placeholder, step, max, min, getFieldDecorator, initialValue, name, labelCol, label}) => {


    //常用校验（特殊校验需要自己处理）
    const maxCheck = (rule: object, value: string | number, callback: any) => {
        if (value && Number(getFieldValue && getFieldValue(name)) > (max || 0)) {
            callback('最大值为' + max);
        } else {
            callback();
        }
    }

    const minCheck = (rule: object, value: string | number, callback: any) => {
        if (value && Number(getFieldValue && getFieldValue(name)) < (min || 0)) {
            callback('最小值为' + min);
        } else {
            callback();
        }
    }

    let checkRules = [];
    if (required) {
        checkRules.push({required, message: '必填项'})
    }
    if (max) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => maxCheck(rule, value, callback)
            }
        )
    }

    if (min) {
        checkRules.push({
                validator: (rule: object, value: string | number, callback: any) => minCheck(rule, value, callback)
            }
        )
    }

    return (
        <Group spans={[span || 16]} textAligns={[textAlign || 'left']}>
            <FormItem labelCol={{span: labelCol || 0}} wrapperCol={{span: 24 - (labelCol || 0)}} label={label}>
                {getFieldDecorator(name, {
                    initialValue,
                    rules: concat(checkRules, rules || []),
                })(<InputNumber style={{width: width || '100%'}}
                                placeholder={placeholder}
                                autoComplete="off"
                                min={min}
                                max={max}
                                step={step || 1}
                                disabled={disabled}
                />)}
            </FormItem>
        </Group>
    )
}

export default FormInputNumber